/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EkranEdycjiPacjenta.java
 *
 * Created on 2011-11-20, 22:51:23
 */
package przychodnia;

import DTO.Pacjenci;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author konp
 */
public class EkranEdycjiPacjenta extends javax.swing.JFrame
{
    private Session ses = null;
    private Long id;
    private Pacjenci pacjent = null;
    
    /** Creates new form EkranEdycjiPacjenta */
    public EkranEdycjiPacjenta(Session xses, Long xid)
    {
        ses = xses;
        id = xid;
        initComponents();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        getRootPane().setDefaultButton(okButton);
        
        
        if (id != -1)
        {
            pacjent = (Pacjenci)ses.createCriteria(Pacjenci.class)
                     .add( Restrictions.eq("id", id)).list().get(0);
            nameTextField.setText(pacjent.getImie());
            surnameTextField.setText(pacjent.getNazwisko());
            peselTextField.setText(pacjent.getPESEL());
            streetTextField.setText(pacjent.getUlica());
            postTextField.setText(pacjent.getPoczta());
            phoneTextField.setText(pacjent.getTelefon());
            genderTextField.setText(pacjent.getPlec());
            mailTextField.setText(pacjent.getMail());
        }
    }
    


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        surnameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        surnameTextField = new javax.swing.JTextField();
        peselLabel = new javax.swing.JLabel();
        streetLabel = new javax.swing.JLabel();
        peselTextField = new javax.swing.JTextField();
        streetTextField = new javax.swing.JTextField();
        postLabel = new javax.swing.JLabel();
        postTextField = new javax.swing.JTextField();
        mailTextField = new javax.swing.JTextField();
        mailLabel = new javax.swing.JLabel();
        phoneLabel = new javax.swing.JLabel();
        phoneTextField = new javax.swing.JTextField();
        genderTextField = new javax.swing.JTextField();
        genderLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Edycja pacjenta"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(przychodnia.Main.class).getContext().getResourceMap(EkranEdycjiPacjenta.class);
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        surnameLabel.setText(resourceMap.getString("surnameLabel.text")); // NOI18N
        surnameLabel.setName("surnameLabel"); // NOI18N

        nameTextField.setName("nameTextField"); // NOI18N
        nameTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameTextFieldActionPerformed(evt);
            }
        });

        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        surnameTextField.setName("surnameTextField"); // NOI18N

        peselLabel.setText(resourceMap.getString("peselLabel.text")); // NOI18N
        peselLabel.setName("peselLabel"); // NOI18N

        streetLabel.setText(resourceMap.getString("streetLabel.text")); // NOI18N
        streetLabel.setName("streetLabel"); // NOI18N

        peselTextField.setText(resourceMap.getString("peselTextField.text")); // NOI18N
        peselTextField.setName("peselTextField"); // NOI18N

        streetTextField.setText(resourceMap.getString("streetTextField.text")); // NOI18N
        streetTextField.setName("streetTextField"); // NOI18N

        postLabel.setText(resourceMap.getString("postLabel.text")); // NOI18N
        postLabel.setName("postLabel"); // NOI18N

        postTextField.setText(resourceMap.getString("postTextField.text")); // NOI18N
        postTextField.setName("postTextField"); // NOI18N

        mailTextField.setText(resourceMap.getString("mailTextField.text")); // NOI18N
        mailTextField.setName("mailTextField"); // NOI18N

        mailLabel.setText(resourceMap.getString("mailLabel.text")); // NOI18N
        mailLabel.setName("mailLabel"); // NOI18N

        phoneLabel.setText(resourceMap.getString("phoneLabel.text")); // NOI18N
        phoneLabel.setName("phoneLabel"); // NOI18N

        phoneTextField.setText(resourceMap.getString("phoneTextField.text")); // NOI18N
        phoneTextField.setName("phoneTextField"); // NOI18N

        genderTextField.setText(resourceMap.getString("genderTextField.text")); // NOI18N
        genderTextField.setName("genderTextField"); // NOI18N

        genderLabel.setText(resourceMap.getString("genderLabel.text")); // NOI18N
        genderLabel.setName("genderLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(175, Short.MAX_VALUE)
                .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(cancelButton)
                .addGap(122, 122, 122))
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(phoneLabel)
                    .addComponent(peselLabel)
                    .addComponent(streetLabel)
                    .addComponent(nameLabel)
                    .addComponent(surnameLabel)
                    .addComponent(postLabel)
                    .addComponent(mailLabel)
                    .addComponent(genderLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(streetTextField)
                    .addComponent(peselTextField)
                    .addComponent(nameTextField)
                    .addComponent(surnameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                    .addComponent(postTextField)
                    .addComponent(mailTextField)
                    .addComponent(phoneTextField)
                    .addComponent(genderTextField))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(surnameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(surnameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(nameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(peselLabel)
                    .addComponent(peselTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(streetLabel)
                    .addComponent(streetTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(postLabel)
                    .addComponent(postTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(mailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mailLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phoneLabel)
                    .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(genderTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(genderLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
    {//GEN-HEADEREND:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_nameTextFieldActionPerformed
    {//GEN-HEADEREND:event_nameTextFieldActionPerformed

        // TODO add your handling code here:}//GEN-LAST:event_nameTextFieldActionPerformed
    }
        private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
    {//GEN-HEADEREND:event_okButtonActionPerformed

        ses.beginTransaction();
        if (id == -1)
             pacjent = new Pacjenci();

        pacjent.setImie(nameTextField.getText());
        pacjent.setNazwisko(surnameTextField.getText());
        pacjent.setPESEL(peselTextField.getText());
        pacjent.setTelefon(phoneTextField.getText());
        pacjent.setPlec(genderTextField.getText());
        pacjent.setMail(mailTextField.getText());
        pacjent.setPoczta(postTextField.getText());
        pacjent.setUlica(streetTextField.getText());
        ses.save(pacjent);
        ses.getTransaction().commit();
        
        dispose();
    }//GEN-LAST:event_okButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(EkranEdycjiPacjenta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(EkranEdycjiPacjenta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(EkranEdycjiPacjenta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(EkranEdycjiPacjenta.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                //new EkranEdycjiPacjenta(ses).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel genderLabel;
    private javax.swing.JTextField genderTextField;
    private javax.swing.JLabel mailLabel;
    private javax.swing.JTextField mailTextField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel peselLabel;
    private javax.swing.JTextField peselTextField;
    private javax.swing.JLabel phoneLabel;
    private javax.swing.JTextField phoneTextField;
    private javax.swing.JLabel postLabel;
    private javax.swing.JTextField postTextField;
    private javax.swing.JLabel streetLabel;
    private javax.swing.JTextField streetTextField;
    private javax.swing.JLabel surnameLabel;
    private javax.swing.JTextField surnameTextField;
    // End of variables declaration//GEN-END:variables
}
